package com.ibeeking.found.content.cp.rest.service.impl;

import com.ibeeking.found.content.cp.rest.service.IBroadcastCouponService;
import com.ibeeking.found.content.cp.rest.service.IBroadcastService;
import com.ibeeking.found.content.service.common.dos.BroadcastCouponDO;
import com.ibeeking.found.content.service.common.param.BroadcastCouponParam;
import com.ibeeking.found.content.service.common.query.BroadcastCouponQuery;
import com.ibeeking.found.content.service.common.vo.BroadcastCouponVO;
import com.ibeeking.found.content.service.mapper.BroadcastCouponMapper;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ClassName BroadcastCouponServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2022-02-18 14:14
 **/
@Service
public class BroadcastCouponServiceImpl extends ServiceImpl<BroadcastCouponMapper, BroadcastCouponDO> implements IBroadcastCouponService {

    @Resource
    private BroadcastCouponMapper broadcastCouponMapper;
    @Resource
    private IBroadcastService broadcastServiceImpl;

    @Override
    public List<BroadcastCouponVO> list(BroadcastCouponQuery broadcastCouponQuery) {
        List<BroadcastCouponDO> broadcastCouponDOs = broadcastCouponMapper.selectList(new LambdaQueryWrapper<BroadcastCouponDO>()
                .eq(broadcastCouponQuery.getId() != null, BroadcastCouponDO::getId, broadcastCouponQuery.getId())
                .in(Collections3.isNotEmpty(broadcastCouponQuery.getIds()), BroadcastCouponDO::getId, broadcastCouponQuery.getIds())
                .like(StringUtils.isNotBlank(broadcastCouponQuery.getCouponName()), BroadcastCouponDO::getCouponName, broadcastCouponQuery.getCouponName())
                .orderByDesc(BroadcastCouponDO::getCreateTime)
        );
        return JsonUtils.jsonToList(broadcastCouponDOs, BroadcastCouponVO.class);
    }

    @Override
    public BroadcastCouponVO view(Long id) {
        BroadcastCouponDO broadcastCouponDO = broadcastCouponMapper.selectById(id);
        return BeanUtil.convertBean(broadcastCouponDO, BroadcastCouponVO.class);
    }

    /**
     * 校验唯一性
     *
     * @param param
     */
    public void checkUnique(BroadcastCouponParam param) {
        LambdaQueryWrapper<BroadcastCouponDO> queryWrapper = new LambdaQueryWrapper<BroadcastCouponDO>()
                .ne(param.getId() != null, BroadcastCouponDO::getId, param.getId())
                .eq(StringUtils.isNotBlank(param.getCouponName()), BroadcastCouponDO::getCouponName, param.getCouponName());
        List<BroadcastCouponDO> dos = broadcastCouponMapper.selectList(queryWrapper);
        if (Collections3.isNotEmpty(dos)) {
            throw new BusinessException("已经存在相同名称的分组!");
        }
    }
}
